﻿using System;
using System.Windows.Forms;

namespace SharpDepend.WPF
{
    internal class DialogManager : IDialogManager
    {
        void Invoke(Action action)
        {
#if WPF
            System.Windows.Application.Current.Dispatcher.Invoke(() =>
            {
                action();
            });
#else
            action();
#endif
        }

        public override void Show(string message, string title, DialogButtons dialogButtons, DialogIcon dialogIcon, DialogResultCallback result, object parameter)
        {
            Invoke(() =>
            {
                MessageBoxButtons buttons = (MessageBoxButtons)0;
                switch (dialogButtons)
                {
                    case DialogButtons.OK:
                        buttons = MessageBoxButtons.OK;
                        break;
                    case DialogButtons.YesNo:
                        buttons = MessageBoxButtons.YesNo;
                        break;
                    default:
                        break;
                }
                MessageBoxIcon icon = (MessageBoxIcon)0;
                switch (dialogIcon)
                {
                    case DialogIcon.None:
                        icon = MessageBoxIcon.None;
                        break;
                    case DialogIcon.Warning:
                        icon = MessageBoxIcon.Warning;
                        break;
                    default:
                        break;
                }

                DialogResult dialogResult = System.Windows.Forms.MessageBox.Show(message, title, buttons, icon);
                
                DialogButtonResult callbackResult = DialogButtonResult.No;
                switch (dialogResult)
                {
                    case DialogResult.OK:
                        callbackResult = DialogButtonResult.OK;
                        break;
                    case DialogResult.Yes:
                        callbackResult = DialogButtonResult.Yes;
                        break;
                    default:
                        break;
                }

                result?.Invoke(callbackResult, parameter);
            });
        }

        public override void OpenFile(string title, string extensions, string defaultPath, DialogOpenSaveFileDialoigCallback result, object parameter)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = extensions;

            if (defaultPath != null)
            {
                dialog.InitialDirectory = defaultPath;
            }

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                result(true, dialog.FileName, parameter);
            }

            result(false, dialog.FileName, parameter);
        }

        public override void SaveFile(string title, string extensions, string defaultPath, DialogOpenSaveFileDialoigCallback result, object parameter)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = extensions;

            if (defaultPath != null)
            {
                dialog.InitialDirectory = defaultPath;
            }

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                result(true, dialog.FileName, parameter);
            }

            result(false, dialog.FileName, parameter);
        }

        public override void OpenFolder(string title, string defaultPath, DialogOpenSaveFileDialoigCallback result, object parameter)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                result(true, dialog.SelectedPath, parameter);
            }

            result(false, dialog.SelectedPath, parameter);
        }

        public override void Input(string label, string title, string defaultInput, DialogInputDialoigCallback result, object parameter)
        {
            InputDialog dialog = new InputDialog(label, title, defaultInput, result, parameter);
            if (dialog.ShowDialog() == true)
            {
                result(true, dialog.Input.Text, parameter);
            }
            else
            {
                result(false, dialog.Input.Text, parameter);
            }
        }
    }
}
